﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ZWZ.Web.WebAdmin.Update
{
    public partial class UpdatePageInfo : System.Web.UI.Page
    {
        private const string DEFAULTIMGURL = "/webadmin/images/nopic2.jpg";
        private const string IMAGEFILEDIRID = "";
        protected string PageID
        {
            get
            {
                if (ViewState["PageID"] == null)
                {
                    if (string.IsNullOrEmpty(Request["pid"]))
                    {
                        ViewState["PageID"] = Codes.IDHelper.CreateID_PageInfo();
                    }
                    else
                    {
                        ViewState["PageID"] = Request["pid"];
                    }
                    ViewState["PageID"] = Request["pid"];
                }
                return ViewState["PageID"].ToString();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["pid"]))
                {

                }
                else
                {
                    Image1.ImageUrl = DEFAULTIMGURL;
                }
            }
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(Request["pid"]))
                {
                    var PInfo = new ModelBase.Page_Info();
                    PInfo.Name = txtName.Text;
                    PInfo.PID = SelPages1.SelectPageID;
                    PInfo.Body_Width = Convert.ToInt32(txtWidth.Text);
                    PInfo.BackGround_color = txtBgColor.Text;
                    PInfo.BackGround_position = txtBgPos.Text;
                    PInfo.BackGround_repeat = ddlBgRpt.SelectedValue;
                    PInfo.BackGround_attachment = ddlBgScroll.SelectedValue;
                    PInfo.BackGround_image = Image1.ImageUrl;
                    PInfo.Font_Size = txtFontSize.Text;
                    PInfo.Font_Family = txtFontName.Text;
                    PInfo.Body_Style = txtStyle.Text;
                    DBProvider.DB.Ctx.Page_Info.AddObject(PInfo);
                    DBProvider.DB.Ctx.SaveChanges();
                }
                else
                {
                    var PInfo = DBProvider.DB.Ctx.Page_Info.Where(i => i.ID == PageID).FirstOrDefault();
                    PInfo.Name = txtName.Text;
                    PInfo.PID = SelPages1.SelectPageID;
                    PInfo.Body_Width = Convert.ToInt32(txtWidth.Text);
                    PInfo.BackGround_color = txtBgColor.Text;
                    PInfo.BackGround_position = txtBgPos.Text;
                    PInfo.BackGround_repeat = ddlBgRpt.SelectedValue;
                    PInfo.BackGround_attachment = ddlBgScroll.SelectedValue;
                    PInfo.BackGround_image = Image1.ImageUrl;
                    PInfo.Font_Size = txtFontSize.Text;
                    PInfo.Font_Family = txtFontName.Text;
                    PInfo.Body_Style = txtStyle.Text;
                    DBProvider.DB.Ctx.SaveChanges();
                }
            }
            catch (Exception exp)
            {
                new Codes.MessageBox(this).Show(exp.Message).Send(); ;
            }
        }
        protected void BtnUpLoad_Click(object sender, EventArgs e)
        {
            try
            {
                if (!fleBgImg.HasFile) { throw new Exception("请选择文件！"); }
                Image1.ImageUrl = Codes.UploadFileHelper.UpLoadFile(IMAGEFILEDIRID, PageID, fleBgImg.PostedFile, Codes.UpLoadFileType.Image);
            }
            catch (Exception exp)
            {
                new Codes.MessageBox(this).Show(exp.Message).Send();
            }
        }
    }
}